<?php

namespace app\api\controller\v1;

use app\common\controller\Api;
use app\common\model\Classes;
use app\common\model\CourseDetail;

/**
 * 教师
 *
 * Class Teacher
 * User zhenzihan
 * Date 2020/6/16
 * @package app\api\controller\v1
 */
class Teacher extends Api
{
    /**
     * 课程表
     *
     * @User zhenzihan
     * @Date 2020/6/16
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function course()
    {
        $teacher_id = $this->request->request('id');
        $semester_id = $this->request->request('semester_id', 0);
        $semester_num = $this->request->request('semester_num', 0);

        $teacher = \app\common\model\Teacher::get($teacher_id);
        if (!$teacher) {
            $this->error('教师不存在');
        }

        //教师任课
        $teaches = \app\common\model\Teach::alias('teach')
            ->join('__SUBJECT__ subject', 'subject.id = teach.subject_id')
            ->where('teach.school_id', $teacher->school_id)
            ->where('teach.teacher_id', $teacher_id)
            ->column('teach.id,teach.type,subject.name subject_name', 'teach.id');

        $teach_ids = array_keys($teaches);
        $course_details = [];
        $list = CourseDetail::whereIn('teach_id', $teach_ids)
            ->where('semester_id', $semester_id)
            ->where('semester_num', $semester_num)
            ->select();

        if ($list) {

            $class_ids = array_column($list->toArray(), 'class_id');

            $classes = [];
            if ($class_ids) {
                $classes = Classes::alias('class')
                    ->join('__GRADE__ grade', 'grade.id = class.grade_id')
                    ->join('__CLASSROOM__ room', 'room.id = class.classroom_id')
                    ->join('__BUILDING__ building', 'building.id = room.building_id')
                    ->whereIn('class.id', $class_ids)
                    ->column('class.id,class.name class_name,grade.name grade_name,building.name building_name,room.name classroom_name', 'class.id');
            }

            foreach ($list as $key => $value) {
                if (isset($teaches[$value['teach_id']]) && isset($classes[$value['class_id']])) {
                    $course_details[$value['number']][$value['day']] = [
                        'class_name' => $classes[$value['class_id']]['class_name'],
                        'grade_name' => $classes[$value['class_id']]['grade_name'],
                        'building_name' => $classes[$value['class_id']]['building_name'],
                        'classroom_name' => $classes[$value['class_id']]['classroom_name'],
                        'subject_name' => $teaches[$value['teach_id']]['subject_name'],
                    ];
                }
            }
        }

        //所有课节
        $class_days = [];
        $list = \app\common\model\ClassDay::where('school_id', $teacher->school_id)->select();
        foreach ($list as $key => $class_day) {
            /*if (isset($course_details[$class_day['number']]) && isset($course_details[$class_day['number']][$class_day['day']])) {
                $class_days[$class_day['number']][$class_day['day']][] = $course_details[$class_day['number']][$class_day['day']];
            } else {
                $class_days[$class_day['number']][$class_day['day']] = [];
            }*/
            $class_days[$class_day['number']]['number'] = $class_day['number'];
            $class_days[$class_day['number']]['start_time'] = $class_day['start_time'];
            $class_days[$class_day['number']]['end_time'] = $class_day['end_time'];
            if (isset($course_details[$class_day['number']]) && isset($course_details[$class_day['number']][$class_day['day']])) {
                $temp_data = $course_details[$class_day['number']][$class_day['day']];
                $class_days[$class_day['number']]['data'][] = [
                    'subject_name' => $temp_data['subject_name'],
                    'grade_name' => $temp_data['grade_name'],
                    'class_name' => $temp_data['class_name'],
                    'building_name' => $temp_data['building_name'],
                    'classroom_name' => $temp_data['classroom_name'],
                ];
            } else {
                $class_days[$class_day['number']]['data'][] = [];
            }
        }
        $class_days = array_values($class_days);

        $this->success('', $class_days);
    }
}
